package com.gree.common.service.impl;

import com.gree.common.entity.ReturnVo;
import com.gree.common.entity.SysApiRoleVo;
import com.gree.common.entity.SysDictData;
import com.gree.common.service.ISysLogininforService;
import com.gree.common.service.SysApiRoleService;
import com.gree.common.utils.Constants;
import com.gree.common.utils.DictUtils;
import com.gree.common.utils.RedisCache;
import com.gree.common.utils.StringUtils;
import com.gree.common.utils.spring.SpringxUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.nutz.dao.Dao;
import org.nutz.dao.entity.Record;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service
public class SysApiRoleServiceImpl implements SysApiRoleService
{

    @Autowired
    private Dao dao;

    @Resource(name = "redisTemplateDb1")
    private RedisTemplate redisTemplate;

    public static String getCacheKey(String configKey)
    {
        return Constants.SYS_API_KEY + configKey;
    }

    public List<SysApiRoleVo> getApiCache(String key)
    {
        ValueOperations<String, Object> operation = redisTemplate.opsForValue();
        Object cacheObj = operation.get(getCacheKey(key));
//        Object cacheObj = SpringxUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key));
        if (StringUtils.isNotNull(cacheObj))
        {
            List<SysApiRoleVo> sysApiRoleVos = StringUtils.cast(cacheObj);
            return sysApiRoleVos;
        }
        return null;
    }

    @Override
    public List<SysApiRoleVo> querySysApiRole(SysApiRoleVo sysApiRoleVo) {
        List<SysApiRoleVo> sysApiRoleVos = getApiCache("2");
        if (StringUtils.isNotEmpty(sysApiRoleVos))
        {
            return sysApiRoleVos;
        }
        Condition cnd = null;
        cnd = Cnd.where("1", "=", "1");
        if(StringUtils.isNotEmpty(sysApiRoleVo.getApiurl()) && sysApiRoleVo.getApiurl()!=null){
            ((Cnd) cnd).and("apiurl", "=", sysApiRoleVo.getApiurl());
        }
        if(StringUtils.isNotEmpty(sysApiRoleVo.getMethod()) && sysApiRoleVo.getMethod()!=null){
            ((Cnd) cnd).and("method", "=", sysApiRoleVo.getMethod());
        }
        if(StringUtils.isNotEmpty(sysApiRoleVo.getRoid()) && sysApiRoleVo.getRoid()!=null){
            ((Cnd) cnd).and("roid", "=", sysApiRoleVo.getRoid());
        }
        if(StringUtils.isNotEmpty(sysApiRoleVo.getToken()) && sysApiRoleVo.getToken()!=null){
            ((Cnd) cnd).and("token", "=", sysApiRoleVo.getToken());
        }
        if(sysApiRoleVo.getType()!=0){
            ((Cnd) cnd).and("type", "=", sysApiRoleVo.getType());
        }
        sysApiRoleVos = dao.query(SysApiRoleVo.class,cnd);
        if (StringUtils.isNotEmpty(sysApiRoleVos))
        {
            for(SysApiRoleVo sysApiRoleVo1:sysApiRoleVos){
                if(sysApiRoleVo1.getType() == 5){
                    List<Record> list = dao.query("sys_api_user",Cnd.where("api_id","=",sysApiRoleVo1.getId()));
                    ArrayList<String> list1 = new ArrayList<String>();
                    for(Record record:list){
                        list1.add(record.getString("username"));
                    }
                    String[] users = list1.toArray(new String[list1.size()]);
                    sysApiRoleVo1.setUsers(users);
                }
            }
            SpringxUtils.getBean(RedisCache.class).setCacheObject(getCacheKey("2"), sysApiRoleVos);
            return sysApiRoleVos;
        }
        return null;
    }

    @Override
    public List<Record> querySysApiUser(Long apiId) {
        List<Record> list = dao.query("sys_api_user",Cnd.where("api_id","=",apiId));
        return list;
    }

    @Override
    public ReturnVo insertSysApiRole(List<SysApiRoleVo> sysApiRoleVos) {
        return null;
    }

    @Override
    public ReturnVo updateSysApiRole(List<SysApiRoleVo> sysApiRoleVos) {
        return null;
    }

    @Override
    public void clearCache() {
        clearApiCache();
    }

    /**
     * 清空字典缓存
     */
    public static void clearApiCache()
    {
        Collection<String> keys = SpringxUtils.getBean(RedisCache.class).keys(Constants.SYS_API_KEY+"2");
        SpringxUtils.getBean(RedisCache.class).deleteObject(keys);
    }
}
